#! /usr/bin/python
# -*- coding: UTF-8 -*-

"""MyTwitter application"""
import os
import os.path

import simplejson
import sched, threading, time
import calendar
import ConfigParser

from twyt import twitter, data, config

# Bootstrap the application
if os.path.isfile(__BASE_DIR__ + 'bootstrap.py'):
    execfile(__BASE_DIR__ + 'bootstrap.py')

class MyTwitter():
    def __init__(self, parent, id):
        # Create main window
        
        self.CenterOnScreen()
        self.panel = wx.Panel(self)
        print "guugui"
        # Get config        
        self.opt_twitter_username = self.ConfigGet('twitter_username', 'Twitter')
        self.opt_twitter_password = self.ConfigGet('twitter_password', 'Twitter')

        # Instantiate twitter class
        self.twit_api = twitter.Twitter()
        self.twit_api.set_user_agent(__twitter_agent__)
        self.twit_api.set_auth(self.opt_twitter_username, self.opt_twitter_password)
        
        # Get known tweet users
        self.tweet_users = {}
        
        # Get events
        self.events = events(self)

        # Create menu bar
        self.main_menu = menu(self, menu_array)
        
        # Create status bar
        self.CreateStatusBar()
        self.SetStatusText(__doc__)
        
        # Create application and taskbar icon
        self.ChangeAppIcon(png_icon)

        self.statuses = StatusBox(self)
        
        # Schedule refreshing of the status
        #self.scheduler = sched.scheduler(time.time, time.sleep)
        self.statuses.RefreshStatusWidget()        

        # Bind some other events (like F5 for refresh)
        self.Bind(wx.EVT_ICONIZE, self.events.OnIconize)

  
    def CreateTimelinePanel(self, window):
        """Timeline for other peoples tweets"""
        self.status_box = wx.Panel(window)
        self.status_box.sizer = wx.FlexGridSizer(1, 3, 3, 3)
        self.status_box.SetSizer(self.status_box.sizer)
        
    def CreateTweetBox(self, window):
        """Set your status text box"""
        self.tweet_box = wx.TextCtrl(window, -1, "", style=wx.TE_PROCESS_ENTER|wx.TE_LEFT)
        self.tweet_box.SetToolTipString("Enter your tweet")
        self.tweet_box.SetFocus()
        self.tweet_box.Bind(wx.EVT_TEXT_ENTER, self.events.OnTweetEnter)

    def ChangeAppIcon(self, icon_file):
        self.Log('Changing application icon to ' + icon_file)
        self.tbicon = wx.TaskBarIcon()
        image = wx.Image(icon_file)
        image.Rescale(24,24)

        bmp = image.ConvertToBitmap()
        bmp.SetMask(wx.Mask(bmp, wx.WHITE)) #sets the transparency colour to white

        icon = wx.EmptyIcon()
        icon.CopyFromBitmap(bmp)
        
        self.tbicon.SetIcon(icon, __app_name__)

        self.tbicon.Bind(wx.EVT_TASKBAR_LEFT_UP, self.events.OnTaskBarLeftClick)
        self.tbicon.Bind(wx.EVT_TASKBAR_RIGHT_UP, self.events.OnTaskBarRightClick)

        self.SetIcon(icon)

    def Config(self):
        # See if the directory exist
        if not os.path.exists(__CONFIG_DIR__):
            os.mkdir(__CONFIG_DIR__, 0600)
            
        self.config = ConfigParser.SafeConfigParser()
        self.config.read(__config_file__)

    def ConfigSet(self, option, value, section='Global'):
        if not hasattr(self, 'config'):
            self.Config()
        if not self.config.has_section(section) and section != '':
            self.config.add_section(section)
        if type(value) == type(u''):
            value = value.encode("utf-8")
        self.config.set(section, option, str(value))
        with open(__config_file__, 'wb') as configfile:
            self.config.write(configfile)

    def ConfigGet(self, option, section='Global'):
        if not hasattr(self, 'config'):
            self.Config()
        if self.config.has_section(section):
            value = self.config.get(section, option)
            return value.decode("utf-8")
        return False

    def Log(self, string):
        if self.ConfigGet('logging') == 'enable':
            print string